package sharetime.tag.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import sharetime.core.EntityObject;

@Entity
@Table(name = "tagged_entity")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class TaggedEntityObject extends EntityObject {

	protected List<Tag> tags = new ArrayList<Tag>();

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
	@JoinTable(
            name="entity_tag",
            joinColumns = { @JoinColumn( name="entity_id") },
            inverseJoinColumns = {@JoinColumn( name="tag_id")}
    )
    
	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	public void addTag(Tag tag) {
		tags.add(tag);
		tag.getTaggedEntities().add(this);
	}
}
